<?php
class ArticleModel extends Model
{
	/**
	 * 添加文章
	 * @param array $art 文章信息
	 */
	public function insertArt($art)
	{
		extract($art);
		// $thumb='default.jpg';
		$addtime=time();
		$sql="insert into bg_article values(null,$cate_id,'$title','$thumb','$art_desc','$content','author',default,default,$addtime,default)";
		return $this->dao->my_query($sql);
	}
	/**
	 * 查询文章在首页展示
	 */
	public function getArticle()
	{
		$pageNum=isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
		$rowsPrePage=$GLOBALS['conf']['Page']['rowsPrePage'];
		$offset=($pageNum-1)*$rowsPrePage;
		$sql="select a.*,c.cate_name from bg_article as a left join bg_category as c on a.cate_id=c.cate_id where a.is_del='0' order by a.addtime desc limit $offset,$rowsPrePage";
		return $this->dao->fetchAll($sql);
	}
	public function getArticleById($art_id)
	{
		$sql="select a.*,c.cate_name from bg_article as a left join bg_category as c on a.cate_id=c.cate_id where art_id=$art_id";
		return $this->dao->fetchRow($sql);
	}
	public function updateArtById($art)
	{
		extract($art);
		$sql="update bg_article set cate_id=$cate_id,title='$title',thumb='$thumb',art_desc='$art_desc',content='$content',author='$author' where art_id=$art_id";
		return $this->dao->my_query($sql);
	}
	public function delArticleById($art_id)
	{
		$sql="update bg_article set is_del='1' where art_id=$art_id";
		return $this->dao->my_query($sql);
	}
	public function delAllArticle($art_id)
	{
		$sql="update bg_article set is_del='1' where art_id in($art_id)";
		return $this->dao->my_query($sql);
	}
	public function getDeledArticle()
	{
		$sql="select a.*,c.cate_name from bg_article as a left join bg_category as c on a.cate_id=c.cate_id where a.is_del='1' order by a.addtime desc";
		return $this->dao->fetchAll($sql);
	}
	public function realDelAllArticle($art_id)
	{
		$sql="delete from bg_article where art_id in($art_id)";
		return $this->dao->my_query($sql);
	}
	public function realDelArtById($art_id)
	{
		$sql="delete from bg_article where art_id =$art_id";
		return $this->dao->my_query($sql);
	}
	public function recoverArtById($art_id)
	{
		$sql="update bg_article set is_del='0' where art_id=$art_id";
		return $this->dao->my_query($sql);
	}
	public function getRowsCount()
	{
		$sql="select count(*) from bg_article where is_del='0'";
		return $this->dao->fetchColumn($sql);
	}

	public function updateArtRecommendById($art_id,$is_recommend)
	{
		if($is_recommend=='0')
		{
			$is_recommend='1';
		}
		else
		{
			$is_recommend='0';
		}
		$sql="update bg_article set is_recommend='$is_recommend' where art_id=$art_id";
		return $this->dao->my_query($sql);
	}
	public function getThumb()
	{
		$sql="select art_id,thumb from bg_article";
		return $this->dao->fetchAll($sql);
	}
}	